package com.zhouyangyang.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhouyangyang.system.constant.SystemConstant;
import com.zhouyangyang.system.dao.AdminMapper;
import com.zhouyangyang.system.pojo.Admin;
import com.zhouyangyang.system.service.AdminService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * @PackageName: com.zhouyangyang.system.service.impl
 * @ClassName: AdminServiceImpl
 * @Author: zhouyangyang
 * @Date: 2020/7/30 23:28
 * @Description:
 */
@Service
@RequiredArgsConstructor
public class AdminServiceImpl implements AdminService {
    private final AdminMapper adminMapper;

    /**
     * 查询所有
     *
     * @return list
     */
    @Override
    public List<Admin> findAll() {
        return adminMapper.selectAll();
    }

    /**
     * 根据id查询
     *
     * @param id 用户id
     * @return Admin
     */
    @Override
    public Admin findById(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    /**
     * 增加用户
     *
     * @param admin 用户参数
     */
    @Override
    @Transactional
    public void add(Admin admin) {
        adminMapper.insert(admin);
    }

    /**
     * 更新用户信息
     *
     * @param admin 用户信息
     */
    @Override
    @Transactional
    public void update(Admin admin) {
        adminMapper.updateByPrimaryKey(admin);
    }

    /**
     * 根据id删除用户信息
     *
     * @param id 用户id
     */
    @Override
    @Transactional
    public void delete(Integer id) {
        adminMapper.deleteByPrimaryKey(id);
    }

    /**
     * 多条件搜索
     *
     * @param searchMap 搜索条件
     * @return list
     */
    @Override
    public List<Admin> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return adminMapper.selectByExample(example);
    }

    /**
     * 构建查询条件模板
     *
     * @param searchMap 查询条件
     * @return example
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Admin.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            //用户名
            String loginName = (String) searchMap.get(SystemConstant.LOGIN_NAME);
            if (loginName != null && !"".equals(loginName)) {
                criteria.andLike(SystemConstant.LOGIN_NAME, "%" + loginName + "%");
            }
            // 密码
            String password = (String) searchMap.get("password");
            if (password != null && !"".equals(password)) {
                criteria.andLike(SystemConstant.PASSWORD, "%" + searchMap.get(SystemConstant.PASSWORD) + "%");
            }
            //状态
            String status = (String) searchMap.get("status");
            if (status != null && !"".equals(status)) {
                criteria.andEqualTo(SystemConstant.STATUS, searchMap.get(SystemConstant.STATUS));
            }
            //id
            Integer id = (Integer) searchMap.get(SystemConstant.ID);
            if (id != null) {
                criteria.andEqualTo(SystemConstant.ID, id);
            }
        }
        return example;
    }

    /**
     * 分页查询
     *
     * @param page 当前页
     * @param size 每页显示条数
     * @return Page<admin>
     */
    @Override
    public Page<Admin> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        return (Page<Admin>) adminMapper.selectAll();
    }

    /**
     * 条件分页查询
     *
     * @param searchMap 搜索条件
     * @param page      当前页
     * @param size      每页显示条数
     * @return Page<admin>
     */
    @Override
    public Page<Admin> findPage(Map<String, Object> searchMap, int page, int size) {
        Example example = createExample(searchMap);
        PageHelper.startPage(page, size);
        return (Page<Admin>) adminMapper.selectByExample(example);
    }

    /**
     * 登录验证密码
     *
     * @param admin 用户信息
     * @return boolean
     */
    @Override
    public Boolean login(Admin admin) {
        Admin admin1 = new Admin();
        admin1.setLoginName(admin.getLoginName());
        admin1.setStatus(SystemConstant.LOGIN_STATUS);
        Admin admin2 = adminMapper.selectOne(admin1);
        if (admin2 == null) {
            return false;
        }
        return BCrypt.checkpw(admin.getPassword(), admin2.getPassword());
    }
}
